<?php
/**
 * 命令行具体执行程序
 * 1)必须放置于cli/script目录下
 * 2)文件名必须为小写的cli_xxxx.php
 * 3)类名必须为Cli_Xxxx格式
 * 4)必须包含一个run($args)方法,$args为自动转换后的命令行参数
 *
 * 调用方法:
 * <code>
 * cli script=demo username=sharmy
 * </code>
 *
 * 命令行应用示例
 *
 * @author Chengtian.Hu<chengtian.hu@gmail.com>
 */

class Cli_tbautopay{
    public function run($args){
    	$ids = array(
    			'20331155139',
    			'18286818217',
    			'18286774453'
    			);
    	set_time_limit(0);
    	$session = $this->_loadtoken();
    	require_once dirname(__FILE__).'/../../lib/top/TopClient.php';
    	require_once dirname(__FILE__).'/../../lib/top/request/TradeFullinfoGetRequest.php';
    	require_once dirname(__FILE__).'/../../lib/top/request/LogisticsDummySendRequest.php';
    	require_once dirname(__FILE__).'/../../lib/top/RequestCheckUtil.php';
    	require_once dirname(__FILE__).'/../../lib/top/request/TradesSoldGetRequest.php';
    	 
    	$c = new TopClient;
    	$c->appkey = '21612259';
    	$c->secretKey = '39a31b83172056116d82ac8af7ad7a4b';
    	$req = new TradesSoldGetRequest();
    	$req->setFields('status,num,total_fee,price,num_iid,receiver_address,tid');
    	$req->setStartCreated(date('Y-m-d',strtotime('-1 day')));
    	$req->setStatus('WAIT_SELLER_SEND_GOODS');
    	$querytaobao = $c->execute($req,$session);
    	Helper_Common::log('TAOBAO-CHECK RESULT:'.print_r($querytaobao,true),__CLASS__);
    	dump(($querytaobao));
    	if(intval($querytaobao->total_results) >0)
    	{
    		if(intval($querytaobao->total_results) == 1)
    		{
    			$trades = array($querytaobao->trades->trade);
    		}else{
    			$trades = $querytaobao->trades->trade;
    		}
    		foreach ($trades as $k=>$v)
    		{
    			
    			if(in_array($v->num_iid, $ids))
    			{
    				if(preg_match('/\d{11}/' ,$v->receiver_address,$reg))
    				{
    					$req = new LogisticsDummySendRequest();
    					$req->setTid($v->tid);
    					$resp = $c->execute($req,$session);
    					if($resp->shipping->is_success){
    						$acctname = $reg[0];
    						$card = array(
    								'cardname'=>$v->tid,
    								'password'=>'123456',
    								'agent_id'=>2855,
    								'admin_id'=>1,
    								'balance' =>$v->price*$v->num,
    								'limitbalance'=>0,
    								'expireddays'=>90*$v->num,
    								'rategroupid'=>9650,
    								'rategroupid_acall'=>9651,
    								'bindexten'=>'',
    								'voucher_acctname'=>'',
    								'payid'=>'taobao_autopay',
    								'expireddate'=>date('Y-m-d',strtotime("+ 90 day")),
    								'voucheredtype'=>2,
    								'cardtype'=>0,
    								'automoney'=>0
    						);
    						$status = Acct::make_card(2855, $card);
    						$status = Acctuser::use_card($acctname, $card['cardname'], $card['password']);
    							
    						if ($status === true)
    						{
    							$acct_name = $acctname;
    							$log = new Syslog();
    							$log->acctname = $acct_name;
    							$log->admin_id = 1;
    							$log->agent_id = 2855;
    							$log->clientip = '';
    							$log->logtext = "后台自动淘宝充值,使用充值卡{$card['cardname']} 为 {$acct_name} 充值{$card['balance']}成功";
    							$log->save(0,'create');
    						}	
    					}
    				}
    			}
    		}
    	}
    	
    }
    function _loadtoken()
    {
    	$taobao = Acct::find('id = 1')->setColumns('id,tbtoken')->getOne();
    	return trim($taobao->tbtoken);
    }
}